package com.example.administrator.activityandintent1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Intent_use1 extends AppCompatActivity {
    /** Called when the activity is first created. */
    RadioButton r1 = null;
    RadioButton r2 = null;
    RadioButton r3 = null;
    RadioButton r4 = null;
    RadioGroup radioGroup = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_use);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        r1 = (RadioButton)findViewById(R.id.a);
        r2 = (RadioButton)findViewById(R.id.b);
        r3 = (RadioButton)findViewById(R.id.c);
        r4 = (RadioButton)findViewById(R.id.d);

        Button bt1_commit = (Button)findViewById(R.id.commit);
        Button bt2_reset = (Button)findViewById(R.id.reset);
        bt1_commit.setOnClickListener(new btn1_commit());
        bt2_reset.setOnClickListener(new  bt2_reset());

    }

    class btn1_commit implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            String ans="";
            if (!(r1.isChecked())&&!(r2.isChecked())&&!(r3.isChecked())&&!(r4.isChecked())){
                Toast.makeText(Intent_use1.this,"Please select anyone.",Toast.LENGTH_SHORT).show();
            }
            else {
                if (r1.isChecked()) {
                    ans = "on";
                } else if (r2.isChecked()) {
                    ans = "at";
                } else if (r3.isChecked()) {
                    ans = "of";
                } else if (r4.isChecked()) {
                    ans = "in";
                }
                Intent intent = new Intent();
                intent.setClass(Intent_use1.this, answerActivity1.class);

                Bundle bundle = new Bundle();
                bundle.putString("ans", ans);
                intent.putExtras(bundle);

                /*
                    法一：
                    Intent_use1.this.startActivity(intent);
                */

                //法二：启动指定Activity并等待返回的结果，其中0是请求码，用于标识该请求
                startActivityForResult(intent , 0);

            }

        }
    }

    class bt2_reset implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            radioGroup.clearCheck() ;
            setTitle("");
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        /*
        法一：
            switch (resultCode) {
                case RESULT_OK:

                /取得来自Activity2 的数据，并显示于画面上
                    Bundle bunde = data.getExtras();
                    String ans = bunde.getString("ans");
                    break;

                default:
                    break;
            }
        */

        //法二：当requestCode、resultCode同时为0，也就是处理特定的结果
        if (requestCode == 0 && resultCode == 0) {
            /* 取得来自Activity2 的数据，并显示于画面上 */
            Bundle bunde = data.getExtras();
            String ans = bunde.getString("ans");
        }
    }

}
